package xin.mydreams.mybill.widget.user.service.impl;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import xin.mydreams.mybill.widget.user.entity.User;
import xin.mydreams.mybill.widget.user.repostory.UserRepository;
import xin.mydreams.mybill.widget.user.service.IUserService;

/**
 * 用户实现类
 * 
 * @Type UserServiceImpl.java
 * @author 谷泽昊
 * @date 2017年11月17日 下午7:20:55
 * @version
 */
@Service
public class UserServiceImpl implements IUserService {
	@Autowired
	private UserRepository userRepository;

	@Override
	public User findByUserName(String userName) {
		List<User> list = userRepository.findByUserName(userName);
		if(list!=null &&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public User findById(String id) {
		Optional<User> optional = userRepository.findById(id);
		return optional.get();
	}

	@Override
	public User save(User user) {
		return userRepository.save(user);
	}

	@Override
	public void deleteByUser(User user) {
		userRepository.delete(user);
	}

	@Override
	public void deleteByUser(List<User> list) {
		userRepository.deleteAll(list);
	}

	@Override
	public List<User> findAll() {
		return userRepository.findAll();
	}


	@Override
	public List<User> updateAll(Collection<User> users) {
		return userRepository.saveAll(users);
	}

	@Override
	public List<User> findByIds(String[] ids) {
		return findByIds(Arrays.asList(ids));
	}

	@Override
	public List<User> findByIds(Collection<String> ids) {
		return userRepository.findAllById(ids);
	}
}

/**
 *
 * Revision history
 * -------------------------------------------------------------------------
 * 
 * Date Author Note
 * -------------------------------------------------------------------------
 * 2017年11月17日 谷泽昊 creat
 */